new Vue({
	el: "body",
	data: {
		indexadress: "",
		adress: [],
		newadress: "",
		integral: "",
		Coupon: [],
		shopimg: "",
		shopname: "",
		products_data: [],
		sendprice: "",
		count: "",
		totle: "",
		username: "",
		user_phonenum: "",
		yhshow: false,
		choiceshow: false,
		itembgc: 0,
		adressiptshow: false,
		mymoney: "",
		userid: "",
		ifnewuser: "",
		actprice: ""
	},
	created: function() {
		this.leancloudInit();
		var orderid = geturl_ch('orderid');
//		var cheapid = geturl_ch('cheapid');
//		if(cheapid) {
//			this.findcheap(cheapid);
//		} else {
			this.findorder(orderid);
//		}
	},
	methods: {
		backpage: function() {
			history.back();
		},
		leancloudInit: function() {
			var APP_ID = 'c4w6bVvWfh8xxcmkBE1G0gIt-gzGzoHsz';
			var APP_KEY = 'JBikg4Ksk76zcTi5BKYWvTJF';
			AV.init({
				appId: APP_ID,
				appKey: APP_KEY
			});
		},
		findorder: function(orderid) {
			//根据距离筛选商家
			var _this = this;
			var query = new AV.Query('ELM_order');
			query.get(orderid).then(function(data) {
				// 成功获得实例
				_this.indexadress = eval(data.attributes.user_adress)[0];
				_this.adress = eval(data.attributes.user_adress);
				_this.shopimg = data.attributes.shopimg;
				_this.shopname = data.attributes.shop_name;
				_this.products_data = data.attributes.products_data;
				_this.sendprice = data.attributes.shop_sendprice;
				_this.count = data.attributes.total;
				_this.totle = (data.attributes.totle) * 1 + (data.attributes.shop_sendprice) * 1;
				_this.actprice = _this.totle;
				_this.username = data.attributes.username;
				_this.user_phonenum = data.attributes.user_phonenum;
				_this.getmymoney();
			}, function(error) {
				// 异常处理
			})
		},
		getmymoney: function() {
			var _this = this;
			var user_phonenum = this.user_phonenum;
			var query = new AV.Query('ELM_user');
			query.contains('phonenum', '' + user_phonenum + '');
			query.find().then(function(data) {
				_this.mymoney = data[0].attributes.money;
				_this.userid = data[0].id;
				_this.integral = data[0].attributes.integral;
				_this.Coupon = eval(data[0].attributes.Coupon);
				_this.ifnewuser = data[0].attributes.ifnewuser;
				if(_this.ifnewuser) {
					var actprice = _this.totle - 20;
					_this.actprice = actprice > 0 ? actprice : 0;
				}
			}, function(error) {
				// 异常处理
			})
		},
		adressiptshowfn: function() {
			this.adressiptshow = true;
		},
		displayadressipt: function() {
			this.adressiptshow = false;
		},
		stopmaopao: function() {
			event.stopPropagation();
		},
		addadress: function() {
			if((this.newadress).length > 0) {
				this.adress.push(this.newadress);
				this.adressiptshow = false;
			} else {
				alert("不能为空");
			}
		},
		choiceAdress: function(index) {
			this.itembgc = index;
			this.indexadress = (this.adress)[index];
			this.choiceshow = false;
		},
		jifen: function() {
			var _this = this;
			var jf = confirm("是否兑换积分");
			if(jf) {
				var userid = this.userid;
				var money = this.mymoney;
				var integral = this.integral;
				var dhmoney = ((integral / 10) + (money * 1)).toString();
				// 第一个参数是 className，第二个参数是 objectId
				var todo = AV.Object.createWithoutData('ELM_user', userid);
				// 修改属性
				todo.set('money', dhmoney);
				todo.set('integral', '0');
				// 保存到云端
				todo.save().then(function() {
					_this.getmymoney();
				});

			}
		},
		daijinjuan: function(index) {
			var userid = this.userid;
			var actprice = this.actprice;
			var Coupon = this.Coupon[index];
			var ifuse = confirm("是否使用" + this.Coupon[index] + "元代金券");
			if(ifuse) {
				if(actprice > 0) {
					this.actprice = (actprice - Coupon) > 0 ? (actprice - Coupon) : 0;
					this.Coupon.splice(index, 1);
					var todo = AV.Object.createWithoutData('ELM_user', userid);
					// 修改属性
					todo.set('Coupon', '[' + this.Coupon + ']');
					// 保存到云端
					todo.save();
				} else {
					alert("已经免单了哦，您可以留到下次使用");
				}
			}
		},
		pay: function() {
			var mymoney = this.mymoney;
			var actprice = this.actprice;
			if(this.mymoney >= this.actprice) {
				var orderid = geturl_ch('id');
				var userid = this.userid;
				var indexadress = this.indexadress;
				var nowmoney = (mymoney * 1) - (actprice * 1);
				var newintegral = Math.ceil(actprice / 10).toString();
				var user = AV.Object.createWithoutData('ELM_user', userid);
				user.set('ifnewuser', 'false');
				user.set('money', nowmoney.toString());
				user.set('integral', newintegral);
				user.save().then(function() {
					var order = AV.Object.createWithoutData('ELM_order', orderid);
					order.set('indexadress', indexadress);
					order.set('if_pay', true);
					order.set('actprice', actprice);
					order.save().then(function() {
						location.href = "../rightorder/rightorder.html?zt=1&id=" + seturl(orderid);
					});
				});
			} else {
				var ifsetmoney = confirm("您的帐户余额不足，是否立即充值");
				if(ifsetmoney) {
					location.href = "../money/money.html";
				}
			}

		}
	}
})